/*
 * @Author: your name
 * @Date: 2022-02-23 14:39:53
 * @LastEditTime: 2022-02-25 13:27:09
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /aone_flutter/lib/main.dart
 */
import 'dart:io';
import 'package:aone_common/common.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:prototype/components/v_loading.dart';
import 'package:prototype/env.dart';
import 'package:prototype/router/router.dart';
import 'package:prototype/theme/custom_theme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:form_builder_validators/localization/l10n.dart';
import 'package:get/get.dart';
import 'events.dart';
import 'lang/nationalize.dart';

// init some service before build app widget
Future<void> initService() async {
  await SpStorage.init();
  initGlobalEvents();

  await Fetch.init(Env().envConfig.appDomain);

  await Get.putAsync(
    () => GlobalService().init(
      darkTheme: CustomTheme.darkTheme,
      lightTheme: CustomTheme.lightTheme,
    ),
  );

  await Get.putAsync(() => UserService().init());
}

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await initService();

  runApp(const MyApp());

  // 透明状态栏
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: _getMaterialApp,
    );
  }

  GetMaterialApp _getMaterialApp() {
    return GetMaterialApp(
      localizationsDelegates: const [
        FormBuilderLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('zh'),
        Locale('en'),
      ],
      debugShowCheckedModeBanner: false,
      title: 'Flutter App',
      translations: Nationalize(),
      locale: const Locale('zh', 'CN'),
      fallbackLocale: const Locale('en', 'US'),
      theme: CustomTheme.lightTheme,
      darkTheme: CustomTheme.darkTheme,
      themeMode: ThemeMode.light,
      initialRoute: '/',
      getPages: routers,
      navigatorObservers: [
        FlutterSmartDialog.observer,
      ],
      builder: FlutterSmartDialog.init(
        loadingBuilder: (String msg, Color background) {
          return customLoading();
        },
      ),
    );
  }
}
